package uml.view.panels;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import uml.util.UMLConstants;
import uml.util.UMLLabel;
import uml.util.UMLTextArea;
import uml.util.listeners.DragListener;
import uml.view.UMLContainerView;

/**
 * ClassViews are specializations of <tt>UMLContainerViews</tt>. They set the correct defaults to 
 * 
 * @author Neil Obetz
 *
 */

public class ClassView extends UMLContainerView
{
	private static final long serialVersionUID = -7576128187348021250L;
	
	
	private static Dimension SIZE = UMLConstants.CLASS_SIZE;
	private static Color COLOR = UMLConstants.CLASS_COLOR;
	private static int NUMBER_OF_FIELDS = 3;
	private BoxLayout layout = new BoxLayout (this, BoxLayout.Y_AXIS);
	private UMLLabel m_nameLabel;
	
	public ClassView ()
	{
		super ();
		setName ("Class");
		setBorder (UMLConstants.NORMAL);
		setupProperties ();
	}
	
	/**
	 * An initialization method used privately to setup the properties associated with
	 * this object.
	 */
	
	private void setupProperties ()
	{
		setSize (SIZE);
		setBackground (COLOR);
		setLayout (layout);
		addFields (NUMBER_OF_FIELDS);
		addDragListener (new DragListener (this));
		setVisible (true);
	}
	
	/**
	 * Adds the specified number of UMLTextAreas to this object.
	 * 
	 * @param numberOfFields.
	 */
	public void addFields (int numberOfFields)
	{
		m_nameLabel = new UMLLabel (getName ());
		m_nameLabel.setLayout (new GridLayout (1,1));
		add (m_nameLabel);
		
		UMLTextArea textArea;
		for (int i = 0; i < numberOfFields; ++i)
		{
			/*  
			 * Setting the border of a text area will make it look bad if the 
			 * Look and Feel property is changed. To add a border, we create a 
			 * JPanel and put a border on it.
			 */
			JPanel textPanel = new JPanel ();
			textPanel.setLayout (new GridLayout (1,1));
			textPanel.setBackground (COLOR);
			textPanel.setBorder (UMLConstants.COMPONENT_BORDER);
			
			textArea = new UMLTextArea ("Default Text");
			textArea.setBackground (UMLConstants.TEXTAREA_COLOR);
			textPanel.add (textArea);
			
			add (textPanel);
		}
	}
	
	/**
	 * Returns the name JLabel associated with this object.
	 * 
	 * @return JLabel label
	 */
	public JLabel getLabel ()
	{
		return (m_nameLabel);
	}
	
	/**
	 * Sets the ClassViews name JLabel.
	 * @param name
	 */
	public void setClassName (String name)
	{
		setName (name);
		m_nameLabel.setText (getName ());
	}
}
